<?php
/**
 * 评价挂件
 * User: yepanpan
 * Date: 2017/12/25
 * Time: 17:31
 */

namespace Todo\Widget;
use Think\Controller;
use Todo\Util\ArrayUtil;

class RateWidget extends Controller
{
    /**
     * 预览页面
     * @param $date array 模板对象
     */
    public function preview($data){
        $list = D('Todo/Rate/Norm')->filterByField('tpl_id',$data['id']);
        $this->assign('data', $data);
        if($data['level'] == 1){
            $this->assign('list', $list);
            $this->display('./Common/View/Widget/rate_preview1.html');
        }else{
            $tree = ArrayUtil::getTree($list);
            $this->assign('tree', $tree);
            $this->display('./Common/View/Widget/rate_preview'.$data['level'].'.html');
        }
    }

    /**
     * 预览页面
     * @param int $projectId 项目ID
     * @param int $userId 打分用户ID
     * @return null
     */
    public function show($projectId, $userId){
        $info = D('Todo/Rate/Project')->keyFind($projectId);
        $data = D('Todo/Rate/Template')->keyFind($info['tpl_id']);
        $list = D('Todo/Rate/Norm')->filterByField('tpl_id',$data['id']);
        $this->assign('data', $data);
        $this->assign('info', $info);
        
        $record = D('Todo/Rate/Record')->checkRecord($projectId, $userId, session('user.id'));
        if(!$record){
            $this->error('发生错误!');
        }else{
            $scores = D('Todo/Rate/Score')->where(array('record_id'=>$record['id']))->select();
            $map = ArrayUtil::map($scores, 'norm_id', 'score');
            $this->assign('map', $map);
        }
        $this->assign('record', $record);
        if($data['level'] == 1){
            $this->assign('list', $list);
            $this->display('./Common/View/Widget/rate_show1.html');
        }else{
            $tree = ArrayUtil::getTree($list);
            $this->assign('tree', $tree);
            $this->display('./Common/View/Widget/rate_show'.$data['level'].'.html');
        }
    }
}